package com.raychi.demo.servlet.buyer;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.raychi.demo.entity.Buyer;
import com.raychi.demo.util.BuyerUtil;
import com.raychi.demo.util.JdbcUtil;

public class BuyerServlet extends HttpServlet{

	
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			List<Buyer> list = getBuyers();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/index2.jsp").forward(request, response);
		} catch (ClassNotFoundException | SQLException e) {
	
			e.printStackTrace();
		}
	}

	
	private List<Buyer> getBuyers() throws ClassNotFoundException, SQLException{
		List<Buyer> buyers = new ArrayList<Buyer>();
		Connection conn = JdbcUtil.getConnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("select * from Buyer");
		Buyer buyer = null;
		while(rs.next()){
			buyer = BuyerUtil.createBuyer(rs);
			buyers.add(buyer);
		}

		return buyers;
	}
}
